/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1
import WebFeeds 1.0
import "UIConstants.js" as UIConstants
import "Utils.js" as Utils

Page {
    id: root
    property bool contentsBusy: false
    property PageItem rootItem
    property Page parentPage
    property Component contents

    function pageForItem(item) {
        return "";
    }

    function showMessage(message) {
        serviceMessageBanner.text = message;
        serviceMessageBanner.show();
    }

    function createPageForItem(item) {
        var nextPageUrl = pageForItem(item);
        if(nextPageUrl) {
            var nextStackPage = Qt.createComponent(nextPageUrl);
            pageStack.push(nextStackPage, {parentPage: root, rootItem: item});
        }
    }

    function showContextMenuForItem(item) {
        var contextMenuItems = webFeedsApp.contextMenuItems(item);
        if(contextMenuItems.length > 0) {
            var menu = Utils.createMenu(contextMenuItems, true, item, root);
            menu.open();
        }
    }

    function changeRootItem(item) {
        if(item && (item != root.rootItem)) {
            var prevRootItem = root.rootItem;
            root.rootItem = item;
            root.rootItem.active = true;
            prevRootItem.active = false;
        }
    }

    onStatusChanged: {        
        if(root.status == PageStatus.Active) {
            root.rootItem.active = true;
        }
    }
    Component.onDestruction: {
        if(root.rootItem)
            root.rootItem.active = false;
    }

    InfoBanner {
        id: serviceMessageBanner
        parent: root.pageStack ? root.pageStack.parent : root
    }

    Connections {
        target: root.rootItem
        onServiceMessage: {
            root.showMessage(message);
        }
        onAboutToBeRemovedFromParent: {
            if(root.status != PageStatus.Deactivating) {
                console.log("onAboutToBeRemovedFromParent " + root.rootItem.title);
                root.pageStack.pop(root.parentPage);
            }
        }
    }

    Loader {
        id: contentsLoader
        anchors.fill: parent
        sourceComponent: root.contents
    }
}
